<?php
/**
 * CK 开发框架
 * User: Clake
 * Date: 15/6/13
 * Time: 04:17
 */

namespace CK\Core;

/**
 * Class Controller
 * 控制器类
 * @package CK\Core
 */
class Controller extends Base {

    private static $_instance;

    /**
     * 模板类
     * @var View
     */
    private $_view;

    //默认模板
    private $_def_tpl;

    /**
     * @return $this
     */
    public static function inst() {
        if (!isset(self::$_instance)) {
            $class = get_called_class();
            self::$_instance = new $class();
        }
        return self::$_instance;
    }

    public function isPost() {
        return $_SERVER['REQUEST_METHOD'] == 'POST' ? true : false;
    }

    public function isGet() {
        return $_SERVER['REQUEST_METHOD'] == 'GET' ? true : false;
    }

    public function isAjax() {
        return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest' ? true : false;
    }

    /**
     * 重定向地址
     *
     * @param string $url
     *        	<重定向地址>
     * @param string $bak_url
     *        	[可选] <返回地址,被重定向之后需要返回的地址>
     */
    public function redirect($url, $bak_url = null) {
        if (preg_match ( "/^http|https|ftp|file/is", $url )) {
            $is_out = true;
            $url_param = parse_url ( $url );
        } else {
            $is_out = false;
            $base_str = $url [0];
            if ($base_str === "/") {
            } else {
                $url = '/' . $url;
            }
            $url = APP_DOMAIN . $url;
            $url_param = parse_url ( $url );
        }
        $re_url = isset($url_param ['path']) ? $url_param['path'] : '';
        if (! empty ( $url_param ['query'] )) {
            $re_url .= '?' . $url_param ['query'];
        }

        if (! empty($url_param['fragment'])) {
            $re_url .= '#' . $url_param ['fragment'];
        }

        if ($bak_url) {
            $sp = ! empty ( $url_param ['query'] ) ? '&' : '?';
            $re_url .= $sp . 'u=' . rawurlencode ( $bak_url );
        }
        if ($is_out) {
            $re_url = $url_param ['scheme'] . '://' . $url_param ['host'] . $re_url;
        }
        header ( "Location:$re_url" );
        exit;
    }

    /**
     * 是否微信访问
     * @return bool
     */
    public function isWechart() {
        $usr_agent = strtolower($_SERVER['HTTP_USER_AGENT']);
        if (stripos($usr_agent,'micromessenger')) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 设置模板引擎
     * @param $view
     */
    public function setView($view) {
        $this->_view = $view;
    }

    /**
     * 设置默认模板
     * @param $tpl
     */
    public function setDefaultTemplate($tpl) {
        $this->_def_tpl = strtolower($tpl);
    }

    /**
     * 设置模板里用的变量
     * @param $key
     * @param $value
     */
    public function assign($key,$value) {
        $this->_view->assign($key,$value);
    }

    /**
     * 显示模板
     * @param null $tpl
     */
    public function display($tpl=null) {
        if ($tpl === null) {
            $tpl = $this->_def_tpl;
        }
        $this->_view->display($tpl);
    }

    /**
     * 返回模板内容
     * @param $tpl
     *
     * @return string
     */
    public function fetch($tpl) {
        return $this->_view->fetch($tpl);
    }
}